<?xml version='1.0' encoding='iso-8859-1'?>

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
                exclude-result-prefixes="fo">
  <xsl:output method="text" indent="no"/>


  <xsl:strip-space elements="*"/>
  <!-- =============================================================== -->
  <!-- Root. Create the bone and call templates for each page sequence -->
  <!-- =============================================================== -->

  <xsl:template match="fo:root">
    <xsl:text>{\rtf1\ansi\fs20{\fonttbl\f0\fswiss Arial;}</xsl:text><!--PAGE LAYOUT-->
    <xsl:text>&#13;&#10;</xsl:text>
    <xsl:text>{\colortbl;\red188\green230\blue138;}</xsl:text><!--COLOR TABLE--><!--TODO:NEED TO MAKE THIS GENERIC-->
    <xsl:text>&#13;&#10;</xsl:text>
    <xsl:apply-templates select="fo:page-sequence"/>
    <xsl:text>}</xsl:text>
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:template>

  <!-- =============================================================== -->
  <!-- fo:page-sequence                                                -->
  <!-- =============================================================== -->
  <xsl:template match="fo:page-sequence">

    <xsl:variable name="current-master">
      <xsl:value-of select="@master-reference"/>
    </xsl:variable>

    <!-- One page master will be used for the whole page sequence -->
    <xsl:variable name="page-master-name">
      <xsl:choose>
        <xsl:when test="../fo:layout-master-set/fo:simple-page-master[@master-name=$current-master]">
          <!-- specified master is a page master: use it -->
          <xsl:value-of select="$current-master"/>
        </xsl:when>
        <xsl:otherwise>
          <!-- specified master is a page sequence master: -->
          <!-- find master name for the first page -->
          <xsl:apply-templates select="../fo:layout-master-set/fo:page-sequence-master[@master-name=$current-master]"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- Load the page master into a variable. No problem if it's null; should work the same ;-) -->
    <xsl:variable name="page-master" select="../fo:layout-master-set/fo:simple-page-master[@master-name=$page-master-name]"/>


    <!-- Header -->
    <xsl:variable name="header-region" select="$page-master/fo:region-before"/>
    <xsl:apply-templates select="fo:static-content[@flow-name = $header-region/@region-name
                              or (@flow-name='xsl-region-before' and not($header-region/@region-name))]">
      <xsl:with-param name="region" select="$header-region"/>
    </xsl:apply-templates>

    <!-- Body -->
    <xsl:apply-templates select="fo:flow">
      <xsl:with-param name="region" select="$page-master/fo:region-body"/>
    </xsl:apply-templates>

    <!-- Footer -->
    <xsl:variable name="footer-region" select="$page-master/fo:region-after"/>
    <xsl:apply-templates select="fo:static-content[@flow-name = $footer-region/@region-name
                              or (@flow-name='xsl-region-after' and not($footer-region/@region-name))]">
      <xsl:with-param name="region" select="$footer-region"/>
    </xsl:apply-templates>

  </xsl:template>

  <!-- =============================================================== -->
  <!-- fo:block                                                        -->
  <!-- =============================================================== -->
  
  <xsl:template match="fo:block">
    <xsl:text>{\pard </xsl:text>
    <xsl:call-template name="applyStyles"/>
    <xsl:text>\par}</xsl:text>
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:template>

  <!-- =============================================================== -->
  <!-- fo:inline                                                       -->
  <!-- =============================================================== -->
  
  <xsl:template match="fo:inline">
    <xsl:call-template name="applyStyles"/>
  </xsl:template>

  <!-- =============================================================== -->
  <!-- fo:table                                     -->
  <!-- =============================================================== -->

  <xsl:template match="fo:table">
    <xsl:text>{\pard </xsl:text>
    <xsl:apply-templates select="node()" />
    <xsl:text>}</xsl:text>
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:template>

  <!-- =============================================================== -->
  <!-- fo:table-row                                    -->
  <!-- =============================================================== -->
  <xsl:template match="fo:table-row">
    <xsl:variable name="rowtype" select="local-name(parent::*)"/>
    <xsl:if test="$rowtype = 'table-header'">
      <xsl:text>\trhdr</xsl:text>
    </xsl:if>
    <xsl:if test="$rowtype = 'table-body'">
      <xsl:text>\trowd</xsl:text>
    </xsl:if>
    <xsl:if test="$rowtype = 'table-footer'">
      <xsl:text>\trowd</xsl:text>
    </xsl:if>
    <xsl:text>&#13;&#10;</xsl:text>
    <xsl:variable name="border" select="../@border-style"/>
    <xsl:variable name="vAlign" select="fo:table-cell/@display-align"/>
    <xsl:for-each select="../../fo:table-column">
      <xsl:apply-templates select="$border" mode="prefix" />
      <xsl:apply-templates select="$vAlign" mode="prefix" />
      <xsl:if test="$rowtype = 'table-header'">
        <xsl:text>\clcbpat1</xsl:text><!--TODO:NEED TO MAKE THIS GENERIC-->
      </xsl:if>
      <xsl:if test="$rowtype = 'table-footer'"><!--TODO:NEED TO MAKE THIS GENERIC-->
        <xsl:text>\clcbpat1</xsl:text>
      </xsl:if>
      <xsl:text>\cellx</xsl:text>
      <xsl:value-of select="sum(preceding-sibling::fo:table-column/@column-width) + @column-width"/>
      <xsl:text>&#13;&#10;</xsl:text>
    </xsl:for-each>
    <xsl:call-template name="applyStyles"/>
    <xsl:text>\row</xsl:text>
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:template>

  <!-- =============================================================== -->
  <!-- fo:table-cell                                    -->
  <!-- =============================================================== -->
  
  <xsl:template match="fo:table-cell">
    <xsl:text>{</xsl:text>
    <xsl:call-template name="applyStyles"/>
    <xsl:text>\intbl\cell</xsl:text>
    <xsl:text>}</xsl:text>
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:template>

  <!-- =============================================================== -->
  <!-- fo:list-block                                                        -->
  <!-- =============================================================== -->

  <xsl:template match="fo:list-block">
    <xsl:text>&#13;&#10;</xsl:text>
    <xsl:text>{\*\pn\pnlvlbody\pnf0\pnindent0\pnstart1\pndec{\pntxta .</xsl:text>
    <xsl:variable name="nested" select="local-name(parent::*)"/>
    <xsl:if test="$nested = 'inline'">
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:text>}}</xsl:text>
    <xsl:call-template name="applyStyles"/>
    <xsl:text>\pard</xsl:text>
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="fo:list-item">
    <xsl:text>&#13;&#10;</xsl:text>
    <xsl:text>{\pntext \tab}\fi-340\li720 </xsl:text>
    <xsl:call-template name="applyStyles"/>
    <xsl:text> \par</xsl:text>
  </xsl:template>
  
  <xsl:template match="fo:list-item-label">
    <!--<xsl:call-template name="applyStyles"/>-->
  </xsl:template>

  <xsl:template match="fo:list-item-body">
    <xsl:call-template name="applyStyles"/>
  </xsl:template>
  


  <!-- =============================================================== -->
  <!-- applying styles      -->
  <!-- =============================================================== -->
  
  <xsl:template match="applyStyles" name="applyStyles">
    <xsl:apply-templates select="@*" mode="prefix" />
    <xsl:apply-templates select="node()" />
      <xsl:apply-templates select="@*" mode="suffix">
        <xsl:sort order="descending"/>
      </xsl:apply-templates>
  </xsl:template>
  
  <!-- =============================================================== -->
  <!-- Styling Attributes (Prefix)      -->
  <!-- =============================================================== -->

  <xsl:template match="@font-weight[.='bold']" mode="prefix">\b </xsl:template>
  <xsl:template match="@font-style[.='italic']" mode="prefix">\i </xsl:template>
  <xsl:template match="@text-underline-style[.='solid']" mode="prefix">\ul </xsl:template>
  <xsl:template match="@text-decoration[.='underline']" mode="prefix">\ul </xsl:template>
  <xsl:template match="@border-style[.='solid']" mode="prefix">\clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs</xsl:template>
  <xsl:template match="@text-align[.='left']" mode="prefix">\ql </xsl:template>
  <xsl:template match="@text-align[.='center']" mode="prefix">\qc </xsl:template>
  <xsl:template match="@text-align[.='right']" mode="prefix">\qr </xsl:template>
  <xsl:template match="@display-align[.='bottom']" mode="prefix">\clvertalb </xsl:template>

  <!-- =============================================================== -->
  <!-- Styling Attributes (Suffix)      -->
  <!-- =============================================================== -->

  <xsl:template match="@font-weight[.='bold']" mode="suffix">\b0</xsl:template>
  <xsl:template match="@font-style[.='italic']" mode="suffix">\i0</xsl:template>
  <xsl:template match="@text-underline-style[.='solid']" mode="suffix">\ul0</xsl:template>
  <xsl:template match="@text-decoration[.='underline']" mode="suffix">\ul0</xsl:template>
  <xsl:template match="@text-align[.='left']" mode="suffix">\ql0</xsl:template>
  <xsl:template match="@text-align[.='center']" mode="suffix">\qc0</xsl:template>
  <xsl:template match="@text-align[.='right']" mode="suffix">\qr0</xsl:template>
  <xsl:template match="@display-align[.='bottom']" mode="suffix"> </xsl:template>

  <!-- =============================================================== -->
  <!-- Color Converter      -->
  <!-- =============================================================== -->
  <!--background-color-->
  <xsl:template match="@background-color">
  <!-- the color hexcode to split into decimal r,g,b components -->
  <xsl:variable name="hexval" select="."/>
    <xsl:text>\red</xsl:text>
    <xsl:call-template name="hexpairs-to-dec">
      <xsl:with-param name="pair" select="1"/>
      <xsl:with-param name="hexval" select="$hexval"/>
    </xsl:call-template>
    <xsl:text>\green</xsl:text>
    <xsl:call-template name="hexpairs-to-dec">
      <xsl:with-param name="pair" select="2"/>
      <xsl:with-param name="hexval" select="$hexval"/>
    </xsl:call-template>
    <xsl:text>\blue</xsl:text>
    <xsl:call-template name="hexpairs-to-dec">
      <xsl:with-param name="pair" select="3"/>
      <xsl:with-param name="hexval" select="$hexval"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="hexpairs-to-dec">
  <xsl:param name="pair" select="1"/>
  <xsl:param name="hexval"/>
  <xsl:variable name="hexpair" select="substring($hexval,$pair * 2 - 1,2)"/>
  <xsl:if test="$hexpair">
    <xsl:variable name="hex" select="'0123456789ABCDEF'"/>
    <xsl:value-of select="(string-length(substring-before($hex, substring($hexpair,1,1))))*16 + string-length(substring-before($hex,substring($hexpair,2,1)))"/>
  </xsl:if>
</xsl:template>
  
  <!--<xsl:template match="/">
    --><!-- the color hexcode to split into decimal r,g,b components --><!--
    <xsl:variable name="hexval" select="'EAEAD9'"/>
    <result>
      <hex>
        <xsl:value-of select="$hexval"/>
      </hex>
      <dec>
        <R>
          <xsl:call-template name="hexpairs-to-dec">
            <xsl:with-param name="pair" select="1"/>
            <xsl:with-param name="hexval" select="$hexval"/>
          </xsl:call-template>
        </R>
        <G>
          <xsl:call-template name="hexpairs-to-dec">
            <xsl:with-param name="pair" select="2"/>
            <xsl:with-param name="hexval" select="$hexval"/>
          </xsl:call-template>
        </G>
        <B>
          <xsl:call-template name="hexpairs-to-dec">
            <xsl:with-param name="pair" select="3"/>
            <xsl:with-param name="hexval" select="$hexval"/>
          </xsl:call-template>
        </B>
      </dec>
    </result>
  </xsl:template>

  <xsl:template name="hexpairs-to-dec">
    <xsl:param name="pair" select="1"/>
    <xsl:param name="hexval"/>
    <xsl:variable name="hexpair" select="substring($hexval,$pair * 2 - 1,2)"/>
    <xsl:if test="$hexpair">
      <xsl:variable name="hex" select="'0123456789ABCDEF'"/>
      <xsl:value-of select="(string-length(substring-before($hex, substring($hexpair,1,1))))*16 + string-length(substring-before($hex,substring($hexpair,2,1)))"/>
    </xsl:if>
  </xsl:template>-->
  
</xsl:stylesheet>